﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace MainForm
{
    public partial class App : Form
    {
        public App()
        {
            InitializeComponent();
        }
        Color backColor, barColor, textColor;


        private void picker_colorBack_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() != DialogResult.Cancel)
            {
                picker_colorBack.BackColor = backColor = colorDialog.Color;
            }
        }
        private void picker_colorBar_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() != DialogResult.Cancel)
            {
                picker_colorBar.BackColor = barColor = colorDialog.Color;
            }
        }

        private void App_Load(object sender, EventArgs e)
        {
            this.backColor = this.picker_colorBack.BackColor = Properties.Settings.Default.backColor;
            this.barColor = this.picker_colorBar.BackColor = Properties.Settings.Default.barColor;
            this.textColor = this.picker_TextColor.BackColor = Properties.Settings.Default.textColor;
            this.txb_StartKey.Text = Properties.Settings.Default.StartKey.ToString();
            this.txb_StartKey.Tag = Properties.Settings.Default.StartKey;
            LoadFileData();
        }

        private void btn_default_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定将颜色设置恢复到默认吗？", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.backColor = this.picker_colorBack.BackColor = Color.FromArgb(252, 112, 8);
                this.barColor = this.picker_colorBar.BackColor = Color.FromArgb(250, 222, 128);
                this.textColor = this.picker_TextColor.BackColor = Color.Blue;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            dynamic selectItem = cblist_BossFile.SelectedItem;
            if (selectItem == null || !(selectItem.Value is GameEvents))
            {
                MessageBox.Show("未选择文件或该文件是无效时间轴文件！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (!(txb_StartKey.Tag is Keys))
            {
                MessageBox.Show("还没有设置启动快捷键！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var events = GameEvents.Load(selectItem.Path);
            if (events.Events.Count == 0)
            {
                MessageBox.Show("该文件不包含任何数据，试试编辑一下？", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return;
            }
            this.Hide();
            Options options = new Options
            {
                BackColor = this.backColor,
                BarColor = this.barColor,
                TextColor = this.textColor,
                GameEvents = GameEvents.Load(selectItem.Path),
                StartKey = (Keys)txb_StartKey.Tag
            };
            StartForm startForm = new StartForm(options);
            startForm.FormClosed += (s, args) =>
            {
                this.Show();
                this.LoadFileData();
            };
            startForm.ShowDialog();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            dynamic selectItem = cblist_BossFile.SelectedItem;
            if (selectItem == null || !(selectItem.Value is GameEvents))
            {
                MessageBox.Show("未选择文件或该文件是无效时间轴文件！", "悟空你又调皮了", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.Hide();
            TimeEdior ediorForm = new TimeEdior(selectItem.Path, selectItem.Value);
            ediorForm.FormClosed += (s, args) =>
            {
                this.Show();
                this.LoadFileData();
            };
            ediorForm.ShowDialog();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            this.Hide();
            TimeEdior ediorForm = new TimeEdior(null, null);
            ediorForm.FormClosed += (s, args) =>
            {
                this.Show();
            };
            ediorForm.ShowDialog();
        }

        private void txb_StartKey_KeyDown(object sender, KeyEventArgs e)
        {
            txb_StartKey.Tag = e.KeyCode;
            txb_StartKey.Text = e.KeyCode.ToString();
        }

        private void App_FormClosed(object sender, FormClosedEventArgs e)
        {
            Properties.Settings.Default.backColor = this.backColor;
            Properties.Settings.Default.barColor = this.barColor;
            Properties.Settings.Default.textColor = this.textColor;
            Properties.Settings.Default.StartKey = txb_StartKey.Tag == null ? Keys.F1 : (Keys)txb_StartKey.Tag;
            Properties.Settings.Default.Save();
        }

        private void picker_TextColor_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() != DialogResult.Cancel)
            {
                picker_TextColor.BackColor = textColor = colorDialog.Color;
            }
        }
        private void LoadFileData()
        {
            cblist_BossFile.Items.Clear();
            DirectoryInfo directoryInfo = new DirectoryInfo(Application.StartupPath);
            var files = directoryInfo.GetFiles("*.timedata", SearchOption.TopDirectoryOnly);
            foreach (var item in files)
            {
                var events = GameEvents.Load(item.FullName);
                if (events is GameEvents)
                {
                    cblist_BossFile.Items.Add(new
                    {
                        Name = events.Name,
                        Path = item.FullName,
                        Value = events
                    });
                }
                else
                {
                    cblist_BossFile.Items.Add(new
                    {
                        Name = item.Name + "--无效文件",
                        Value = -1
                    });
                }
            }
        }
    }
}
